﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HAUtil;

namespace HA.Data
{
    /// <summary>
    /// 表示二维离散点数据
    /// </summary>
    [Serializable]
   public class Scatter2D:DescEntity,ICloneable
    {

        public Scatter2D(string id):base(id)
        {
            _valueNames = new List<string>();
            _dataSet = new List<ScatterPoint>();
        }

        Scatter2DCollection _parentCollection;
        /// <summary>
        /// 父集
        /// </summary>
        public Scatter2DCollection ParentCollection
        {
            get { return _parentCollection; }
            set { _parentCollection = value; }
        }

        List<string> _valueNames;
        /// <summary>
        /// 值集合对应名称
        /// </summary>
        public List<string> ValueNames
        {
            get { return _valueNames; }
            set { _valueNames = value; }
        }

        List<ScatterPoint> _dataSet;

        public List<ScatterPoint> DataSet
        {
            get { return _dataSet; }
            set { _dataSet = value; }
        }

        int _scatterPointIndex;

        public string GetNextScatterPointId()
        {
            return "离散点" + this._scatterPointIndex++;
        }


        /// <summary>
        /// 返回指定名称的数据集
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public DSScatter GetDataSet(string name)
        {
            if (this._valueNames.Contains(name))
            {
                int index = this._valueNames.IndexOf(name);

                DSScatter ds = new DSScatter(name);

                foreach (ScatterPoint item in this._dataSet)
                {
                    DSSPoint pt = new DSSPoint(item.Id,item.Values[index],item.X,item.Y,item.Z);

                    ds.Points.Add(pt);
                    
                }


                return ds;


            }


            return null;
 
        }

        public ScatterPoint GetPoint(string id)
        {
            return this.DataSet.Single<ScatterPoint>(h => h.Id == id);
        }

        public void AddPoint(ScatterPoint sp)
        {
            this._dataSet.Add(sp);
            //通知主程
            if(this._parentCollection!=null)
                this._parentCollection.AskScatter2DChanged(new Scatter2DPointsChangedEventArgs(this.Key.ToString(), sp, ObjectOperationType.ADD));
        }

        public void EditPoint(string id,double x,double y,List<double> values)
        {
            foreach (ScatterPoint item in this._dataSet)
            {
                if (item.Id == id)
                {
                    item.X = x;
                    item.Y = y;

                    if (values != null)
                        item.Values = values;

                    if (this._parentCollection != null)
                        this._parentCollection.AskScatter2DChanged(new Scatter2DPointsChangedEventArgs(this.Key.ToString(), item, ObjectOperationType.MODIFY));
                    return;
                }
            }
        }

        public void RemovePoint(List<string> ids)
        {
            bool isContain =true;
            bool isRemoved = false;
            while(isContain)
            {
                foreach (ScatterPoint item in this._dataSet)
                {
                    if (ids.Contains(item.Id))
                    {
                        this._dataSet.Remove(item);
                        isRemoved = true;
                        break;
                    }
                }
                if (isRemoved) isRemoved = false;
                else isContain = false;
            }

            if (this._parentCollection != null)
                this._parentCollection.AskScatter2DChanged(new Scatter2DPointsChangedEventArgs(this.Key.ToString(),ids));
        }

        public List<ScatterPoint> GetScatterPointsClone()
        {
            List<ScatterPoint> d = new List<ScatterPoint>();
            foreach (ScatterPoint item in this._dataSet)
            {
                d.Add((ScatterPoint)item.Clone());
            }
            return d;
        }

        public void ReSetMembers(Scatter2D data)
        {
            this.DataSet = data.GetScatterPointsClone();
            this.Name = data.Name;
            this.ParentCollection = data.ParentCollection;
            this.ValueNames.Clear();
            this.ValueNames.AddRange(data.ValueNames);
            
        }

        public object Clone()
        {
            Scatter2D s=(Scatter2D)this.MemberwiseClone();
            s.DataSet = GetScatterPointsClone();
            s.ValueNames = new List<string>();
            s.ValueNames.AddRange(_valueNames);

            return s;
        }
    }
}
